package vg.modules.backlight.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import vg.services.data_base_manager.data.graph.Attribute;
import vg.services.data_base_manager.data.graph.Edge;
import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;

public class CriticalPathSearcher {
	private JButton button;
	private AGraphView view;
	private JDialog dialog;
	private JComboBox attributeCombobox = new JComboBox();
	private JComboBox directionCombobox = new JComboBox();
	private JCheckBox maxMinCheck = new JCheckBox();
	private Vertex from = null;
	private Vertex to = null;

	// -------------------------------------------------------------------------
	public CriticalPathSearcher() {

		this.button = new JButton(new ImageIcon("./data/resources/textures/critical.png"));
		this.button.setToolTipText("Find critical path beetwen two vertices");
		this.dialog = new JDialog((JFrame) null, "Critical path", true);

		JPanel contentPane = new JPanel(new GridBagLayout());
		dialog.setContentPane(contentPane);
		JButton okButon = new JButton("ok");
		JButton cancelButon = new JButton("cancel");

		GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5,
				10, 5, 5), 0, 0);
		contentPane.add(new JLabel("Attribute"), gbc);
		gbc = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
				100, 0);
		contentPane.add(attributeCombobox, gbc);
		gbc = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
		contentPane.add(new JLabel("Maximum"), gbc);
		gbc = new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
		contentPane.add(maxMinCheck, gbc);
		gbc = new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
		contentPane.add(new JLabel("Direction"), gbc);
		gbc = new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
				0, 0);
		contentPane.add(directionCombobox, gbc);

		JPanel okCancelPane = new JPanel();
		okCancelPane.setLayout(new GridBagLayout());
		gbc = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
				0, 0);
		okCancelPane.add(okButon, gbc);
		gbc = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 10, 5, 5),
				0, 0);
		okCancelPane.add(cancelButon, gbc);

		gbc = new GridBagConstraints(0, 3, 2, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0,
				0);
		contentPane.add(okCancelPane, gbc);
		dialog.setResizable(false);
		dialog.pack();

		okButon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(false);
				String attr = (String) attributeCombobox.getSelectedItem();
				if (directionCombobox.getSelectedIndex() == 0)
					view.getGraphAlgorithms().showCriticalPath(from, to, maxMinCheck.isSelected(), attr);
				else
					view.getGraphAlgorithms().showCriticalPath(to, from, maxMinCheck.isSelected(), attr);
			}
		});

		cancelButon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(false);
			}
		});

		this.button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Graph ssg = view.getGraphInformation().getSelectionSubGraph();
				if (ssg != null) {
					Collection<Vertex> lsv = ssg.getVertices();
					if (lsv != null && lsv.size() == 2) {
						Iterator<Vertex> it = lsv.iterator();
						from = it.next();
						to = it.next();;

						Object[] possibilities = { "from " + from.getStorableId() + " to " + to.getStorableId(),
								"from " + to.getStorableId() + " to " + from.getStorableId() };
						directionCombobox.removeAllItems();
						directionCombobox.addItem(possibilities[0]);
						directionCombobox.addItem(possibilities[1]);
						attributeCombobox.removeAllItems();
						HashSet<String> attrset = new HashSet<String>();
						for (Edge edge : view.getGraphInformation().getGraph().getEdges()) {
							for (Attribute attr : edge.getAttributes()) {
								if (attr.isRealType())
									attrset.add(attr.getName());
							}
						}
						for (String attr : attrset) {
							attributeCombobox.addItem(attr);
						}
						dialog.setVisible(true);
					}
				}
			}
		});
		this.button.setEnabled(false);
	}

	public JComponent getView() {
		return button;
	}

	public void setEnabled(boolean enable) {
		button.setEnabled(enable);
    }

	public void changeView(AGraphView igv) {
		this.view = igv;
		if (view == null) {
			setEnabled(false);
		}
	}
}
